package com.hexb.smh.utils;

import com.hexb.core.utils.ObjectUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * @author : hexb
 */
public class Converters {

    static private final DecimalFormat f = new DecimalFormat("0.00");
    static private final DecimalFormat f2 = new DecimalFormat("#.##");

    static public String decimalAmountFormat(BigDecimal i) {
        if (null == i) return null;
        return f.format(i);
    }

    static public String decimalAmountFormat(String i) {
        if (null == i) return null;
        return f.format(new BigDecimal(i));
    }

    static public BigDecimal stringToDecimal(String s) {
        if (null == s) return null;
        return ObjectUtils.safeCall(() -> new BigDecimal(s), null);
    }

    //百分比和小数之间转换
    static public String rateToPercent(BigDecimal b) {
        return f2.format(b.multiply(new BigDecimal(100)));
    }


}
